package br.com.sales.hanhishi.client.view;

import br.com.sales.generics.client.components.timepicker.HDateTimePicker;
import br.com.sales.hanhishi.client.presenter.HanHisHIPresenter;
import br.com.sales.hanhishi.client.presenter.HanHisHIUiHanlder;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import java.util.Date;

/**
 *
 * @author v
 */
public class HanHisHIView extends ViewWithUiHandlers<HanHisHIUiHanlder> implements HanHisHIPresenter.Display {

    private final static HanHinHIViewUiBinder uiBinder = GWT.create(HanHinHIViewUiBinder.class);
    @UiField
    HTMLPanel container;
    @UiField
    FlowPanel layoutPanel;
    @UiField
    HDateTimePicker initialDate;
    @UiField
    HDateTimePicker finalDate;
    @UiField
    CheckBox ieFiltrarData;
    @UiField
    TextBox filterName;
    @UiField
    Image refreshBtn;
    @UiField
    Button viewReport;

    @Inject
    public HanHisHIView() {
        container = uiBinder.createAndBindUi(this);
    }

    @Override
    public void build() {
        addClickEvent();
        viewReport.setTitle("Preview");
        ieFiltrarData.setText("Filtrar Data");
        ieFiltrarData.setValue(Boolean.TRUE);
        refreshBtn.setResource(HanHisHIResources.INSTANCE.refresh());
        loadDate();
    }

    private void loadDate() {
        Date now = new Date(System.currentTimeMillis());
        DateTimeFormat dtf = DateTimeFormat.getFormat("dd/MM/yyyy");
        finalDate.setValue(dtf.format(now));
        CalendarUtil.addDaysToDate(now, -5);
        initialDate.setValue(dtf.format(now));
    }

    @Override
    public FlowPanel getLayout() {
        return layoutPanel;
    }

    @Override
    public Widget asWidget() {
        return container;
    }

    private void addClickEvent() {
        refreshBtn.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                getUiHandlers().onClickRefresh(event);
            }
        });

        viewReport.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                getUiHandlers().onClickViewReport(event);
            }
        });
    }

    @Override
    public HDateTimePicker getInitialDate() {
        return initialDate;
    }

    @Override
    public HDateTimePicker getFinalDate() {
        return finalDate;
    }

    @Override
    public CheckBox getIeFiltrarData() {
        return ieFiltrarData;
    }

    @Override
    public TextBox getFilterName() {
        return filterName;
    }

    @Override
    public HTMLPanel getContainer() {
        return container;
    }

    interface HanHinHIViewUiBinder extends UiBinder<HTMLPanel, HanHisHIView> {
    }
}
